package com.example.administrator.appli;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;
import android.widget.TextView;

import com.example.administrator.appli.service.RemoteService;
import com.example.administrator.appli.service.ServiceLife;
import com.example.administrator.myapplication.ICountService;

import java.util.Timer;
import java.util.TimerTask;

public class RemoteServiceActivity extends Activity {


    private static final String TAG = "RemoteServiceActivity";
    private ICountService countService;
    private ServiceConnection serviceConnection = new ServiceConnection() {

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            countService = ICountService.Stub.asInterface(service);
            try {
                Log.e("RemoteServiceActivity", "on serivce connected, count is " + countService.getCount());
            } catch (RemoteException e) {
                throw new RuntimeException(e);
            }
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            countService = null;
        }

    };

    TextView tv_remote;
    Intent serviceIntent;
    Timer timer;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_remote_service);
        tv_remote = (TextView) findViewById(R.id.tv_remote);
        Intent serviceIntent = new Intent(this, RemoteService.class);
        bindService(serviceIntent, serviceConnection, Context.BIND_AUTO_CREATE);

         timer =new Timer();timer.schedule(new TimerTask() {
            @Override
            public void run() {
                try {
                    Log.e(TAG, "onCreate: " + countService.getCount());
//                    runOnUiThread(new Runnable() {
//                        @Override
//                        public void run() {
//                            try {
//                                tv_remote.setText(String.valueOf(countService.getCount()));
//                            } catch (RemoteException e) {
//                                e.printStackTrace();
//                                Log.e(TAG, "run: " + e.toString());
//                            }
//                        }
//                    });
                    tv_remote.post(new Runnable() {
                        @Override
                        public void run() {
                            try {
                                tv_remote.setText(String.valueOf(countService.getCount()));
                            } catch (RemoteException e) {
                                e.printStackTrace();
                                Log.e(TAG, "run: " + e.toString());
                            }
                        }
                    });
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }, 1000, 1000);


    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbindService(serviceConnection);
        if(timer!=null){
            timer.cancel();
        }

    }



}
